package com.sponge.controller.aspect;

import com.sponge.utils.LogUtil;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MethodExecuteTime {

    private ThreadLocal<Long> startTime = new ThreadLocal<>();


    @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
    public void allPostMethod() {

    }

    @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
    public void allGetMethod() {

    }

    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void allRequestMethod() {

    }

    @Pointcut("allPostMethod()||allGetMethod()||allRequestMethod()")
    public void allHttpMethod() {

    }

    @Before("allHttpMethod()")
    public void getStartTime() {
        startTime.set(System.currentTimeMillis());
    }

    @AfterReturning("allHttpMethod()")
    public void getEndTime() {
        long endTime = System.currentTimeMillis();
        long spentTime = endTime - startTime.get();
        LogUtil.info(MethodExecuteTime.class, "method execute time == " + spentTime + "ms");

    }

//    @Around("allHttpMethod()")
//    public void returnResult(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//        Object proceed = proceedingJoinPoint.proceed();
//        return new HttpResponseResult<>()
//
//    }

}
